package com.project.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.template.entity.SignRecord;
import com.project.template.exception.CustomException;
import com.project.template.mapper.SignRecordMapper;
import com.project.template.service.SignRecordService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 功能：
 * 作者：lhp
 */
@Service
public class SignRecordServiceImpl extends ServiceImpl<SignRecordMapper, SignRecord> implements SignRecordService {
    @Override
    public Page<SignRecord> page(Map<String, Object> query, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<SignRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(SignRecord::getId);
        return page(new Page<>(pageNum,pageSize),wrapper);
    }

    @Override
    public  boolean isSign(String username, String signNumber){
        LambdaQueryWrapper<SignRecord> wrapper = new LambdaQueryWrapper<>();
        // 根据用户名和签到编号查询是否已存在签到记录
        wrapper.eq(SignRecord::getUsername, username)
                .eq(SignRecord::getSignNumber, signNumber)
                .eq(SignRecord::getState,1); // 可以根据需要调整时间判断
        // 查询最新的签到记录
        SignRecord userInfo = getOne(wrapper);
        // 如果记录不为空，说明该用户在当前签到中有记录，抛出异常
        if (userInfo != null) {
            return true;
        }
        return false;
    }
}
